package com.biz.primus.ms.adv.trans;

import com.biz.primus.model.adv.vo.AdvertisementVo;
import com.biz.primus.ms.adv.dao.po.Advertisement;
import com.google.common.base.Function;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author jun.liu
 * @date 2017/9/18
 */
@Slf4j
public class Advertisement2AdvertisementVo implements Function<Advertisement, AdvertisementVo> {

    @Override
    public AdvertisementVo apply(Advertisement input) {
        AdvertisementVo vo = new AdvertisementVo();
        if (input.getId() != null) {
            vo.setId(String.valueOf(input.getId()));
        }
        vo.setTitle(input.getTitle());
        vo.setAtype(input.getAtype());
        vo.setUrl(input.getUrl());
        vo.setSourcePath(input.getSourcePath());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            ZoneId beginZone = ZoneId.systemDefault();
            Instant beginInstant = input.getBeginDate().atZone(beginZone).toInstant();
            String beginDate = sdf.format(Date.from(beginInstant));
            vo.setBeginDate(beginDate);

            ZoneId endZone = ZoneId.systemDefault();
            Instant endInstant = input.getEndDate().atZone(endZone).toInstant();
            String endDate = sdf.format(Date.from(endInstant));
            vo.setEndDate(endDate);
        } catch (Exception e) {
            log.error("时间转化异常--》" + e.getMessage());
        }
        vo.setSupportedGeoId(input.getSupportedGeoId());
        vo.setCountryStatus(input.isCountryStatus());
        vo.setSecond(input.getSecond());
        return vo;
    }
}
